import { defineStore } from 'pinia'
import { store } from '@/stores'
import {
  ACCESS_TOKEN,
  CURRENT_USER,
  REFRESH_TOKEN,
  REFRESH_TOKEN_TIME,
  IS_LOCKSCREEN,
  IS_REFRESH_TOKEN
} from '@/constants/mutation-types'
import { storage } from '@/utils/Storage'
import { ResultEnum } from '@/enums/httpEnum'

export interface IUserState {
  token: string
  permissions: any[]
  userInfo: {
    [key: string]: any
  }
}

export const useUserStore = defineStore('user', {
  state: (): IUserState => ({
    token: storage.getCookie(ACCESS_TOKEN),
    permissions: [],
    userInfo: storage.get(CURRENT_USER, {})
  }),
  getters: {
    getToken(): string {
      return this.token
    },
    getPermissions(): [any][] {
      return this.permissions
    },
    getUserInfo(): object {
      return this.userInfo
    }
  },
  actions: {
    setToken(token: string) {
      this.token = token
    },
    setPermissions(permissions: any) {
      this.permissions = permissions
    },
    setUserInfo(info: {}) {
      this.userInfo = info
    },
    // 登录
    async Login(userInfo: {}) {
      try {
        storage.set(IS_LOCKSCREEN, false)
        storage.setCookie(ACCESS_TOKEN, 'ACCESS_TOKEN')
        // const response = await login(userInfo)
        // const { data, status } = response
        // if (status == ResultEnum.SUCCESS) {
        //   // 过期时间
        //   const ex = data.expires * 1000
        //   // 刷新时间= 过期时间-200秒
        //   const refreshTime = data.expires - 200
        //   const t = data.tokenHeader + data.accessToken
        //   storage.set(ACCESS_TOKEN, t, ex)
        //   storage.set(REFRESH_TOKEN, data.refreshToken, ex)
        //   storage.set(IS_LOCKSCREEN, false)
        //   storage.set(IS_REFRESH_TOKEN, true)
        //   storage.set(REFRESH_TOKEN_TIME, refreshTime)
        //   this.setToken(t)
        //   this.setRefreshToken(data.refreshToken)
        //   this.setRefresh(true)
        //   this.setRefreshTime(refreshTime)
        // }
        return Promise.resolve('success')
      } catch (e) {
        return Promise.reject(e)
      }
    },
    // 获取用户信息
    async GetUserInfo() {
      const that = this
      return new Promise((resolve, reject) => {
        // getUserInfo()
        //   .then((res: any) => {
        //     const { data, status } = res
        //     if (status === ResultEnum.SUCCESS) {
        //       that.setUserInfo(data)
        //       storage.set(CURRENT_USER, data)
        //       resolve(res)
        //     }
        //     reject(res)
        //   })
        //   .catch((error: any) => {
        //     reject(error)
        //   })
      })
    },

    // 登出
    async Logout() {
      console.log('退出系统')
      this.setPermissions([])
      this.setUserInfo('')
      this.setToken('')
      storage.remove(ACCESS_TOKEN)
      storage.remove(REFRESH_TOKEN)
      storage.remove(CURRENT_USER)
      storage.set(IS_REFRESH_TOKEN, false)
      storage.remove(REFRESH_TOKEN_TIME)
      return Promise.resolve('退出成功')
    }
  }
})

// Need to be used outside the setup
export function useUserStoreWidthOut() {
  return useUserStore(store)
}
